package org.game.module.event.listener;

import org.constant.EventConditionKey;
import org.constant.EventConditionType;
import org.constant.EventType;
import org.game.db.bean.Player;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventListener;
import org.game.plugin.event.GameEventStack;
import org.game.plugin.event.GameEventType;

import com.sojoys.artifact.tools.ToolMap;

/**
 * @author : DengYing
 * @CreateDate : 2018年1月11日 下午2:59:44
 * @Description ：英雄数据变更监听
 */
@GameEventType(EventType.HERO_CHANGE)
public class HeroChangeEventListenner implements GameEventListener {

	@Override
	public void onEvent(GameEvent event) {
		GameEventStack eventStack = event.peek();
		switch (eventStack.getConditionType()) {
		case EventConditionType.HERO_MAX_LVL:
		case EventConditionType.ADVANCED_LVL:{
			if (checkTriggerType(event.getIn(), eventStack.getConditionType())) {
				int oldLv = ToolMap.getInt(EventConditionKey.OLD_LEVEL, event.getIn());
				int nowLv = ToolMap.getInt(EventConditionKey.NOW_LEVEL, event.getIn());
				int progress = eventStack.getMaxProgress();
				if (verify_eq(EventConditionKey.HERO_CID, event.getIn(), eventStack.getParams()) && oldLv < progress) {
					eventStack.setProgress(nowLv);
				}
			}
			
		}
			break;
		case EventConditionType.ADVANCED_LVL_COUNT: {
			if (checkTriggerType(event.getIn(), EventConditionType.ADVANCED_LVL)) {
				int oldLv = ToolMap.getInt(EventConditionKey.OLD_LEVEL, event.getIn());
				int nowLv = ToolMap.getInt(EventConditionKey.NOW_LEVEL, event.getIn());
				int limitLvl = ToolMap.getInt(EventConditionKey.LIMIT_LEVEL, eventStack.getParams());
				if (oldLv < limitLvl && nowLv >= limitLvl) {
					eventStack.changeProgress(1);
				}
			}
		}
			break;	
		case EventConditionType.HERO_UP_LVL_COUNT: {
			if (checkTriggerType(event.getIn(), eventStack.getConditionType())){
				boolean bool = verify_eq(EventConditionKey.HERO_CID, event.getIn(), eventStack.getParams());
				if (bool) {
					eventStack.changeProgress(1);
				}
			}
			break;
		}
		default:
			break;
		}
	}

	@Override
	public void initEventCondition(Player player, GameEventStack eventStack) {
		switch (eventStack.getConditionType()) {
		case EventConditionType.HERO_MAX_LVL: {
			int heroCid = ToolMap.getInt(EventConditionKey.HERO_CID, eventStack.getParams());
			int maxLvl = player.getPlayerProxy().getMaxHeroLvl(heroCid);
			eventStack.setProgress(maxLvl);
		}
			break;
		case EventConditionType.ADVANCED_LVL: {
			int heroCid = ToolMap.getInt(EventConditionKey.HERO_CID, eventStack.getParams());
			int maxLvl = player.getPlayerProxy().getMaxHeroAdvancedLvl(heroCid);
			eventStack.setProgress(maxLvl);
		}
			break;
		case EventConditionType.ADVANCED_LVL_COUNT: {
			int limitLvl = ToolMap.getInt(EventConditionKey.LIMIT_LEVEL, eventStack.getParams());
			int count = player.getPlayerProxy().getMaxHeroAdvancedLvlCount(0 ,limitLvl);
			eventStack.setProgress(count);
		}
		default:
			break;
		}
	}

}
